package com.hsp_java.list_;

import java.util.ArrayList;
import java.util.*;

public class ListExercise02 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Book("红楼梦", "曹雪芹" , 100));
        list.add(new Book("西游记", "吴承恩" , 10));
        list.add(new Book("水浒传", "施耐庵" , 9));

        for (Object l : list) {
            System.out.println(l);
        }

        List linkedList = new LinkedList();
        linkedList.add(new Book("红楼梦", "曹雪芹" , 100));
        linkedList.add(new Book("西游记", "吴承恩" , 10));
        linkedList.add(new Book("水浒传", "施耐庵" , 9));


        linkedList.sort(new Comparator() {
            public int compare(Object o1, Object o2) {
                return (int)(((Book)o1).getPrice() - ((Book)o2).getPrice());
            }
        });

        list.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                //Book b1 = (Book)o1;
                //Book b2 = (Book)o2;
                //
                //return (int)(b1.getPrice() - b2.getPrice());

                return (int)(((Book)o1).getPrice() - ((Book)o2).getPrice());
            }
        });

    }
}

class Book {
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    @Override
    public String toString() {
        return "名称: " + this.name + "\t " + "价格: " + this.price + "\t" + " 作者: " + this.author;
    }

    public double getPrice() {
        return this.price;
    }
}